#include "main.h"
#include <stdlib.h>
#include "mouse.h"
#include "drivers/lv_drivers.h"
#include "./lib/lvgl/lvgl/src/drivers/display/fb/lv_linux_fbdev.h"
#include "mzk-log.h"

lv_screen_info SCREEN_INFO;

lv_display_t * disp = NULL;
lv_obj_t * rm_kb;
lv_obj_t * rm_kb_num;

static void *lv_pth_tick(void *arg)
{
    static uint8_t r = 0;
    reemoon_info("lvgl timer handler start\n");
	while (SYSTEM_RUNNING_FALG)
	{
        usleep(1000);
        #if LV_USE_WAYLAND
            lv_wayland_timer_handler();
        #else
            lv_timer_handler();
        #endif
	}
	return NULL;
}
static void sighandler_handler(int sig)
{
	reemoon_info ("signal: %d!\n", sig);
	if ( sig )
	{
		SYSTEM_RUNNING_FALG = false;
        SYSTEM_SIG_EIXT_FALG = true;
	}
    // usleep(100*1000);
}

static bool lv_wayland_display_close_cb(lv_display_t * disp)
{
        printf ("wayland lvgl close!\n");
        return true;
}

static lv_display_t * hal_init(void)
{
	lv_group_set_default(lv_group_create());

    SCREEN_INFO.DISPLAY_W = 1920;
    SCREEN_INFO.DISPLAY_H = 1080;
    SCREEN_INFO.COLOR_DPP = LV_COLOR_DEPTH;
#if LV_USE_SDL
    disp = lv_sdl_window_create(SCREEN_INFO.DISPLAY_W, SCREEN_INFO.DISPLAY_H);
    lv_display_set_default(disp);

    lv_indev_t * mouse = lv_sdl_mouse_create();
    lv_indev_set_group(mouse, lv_group_get_default());
    lv_indev_set_display(mouse, disp);
    lv_display_set_default(disp);

    LV_IMAGE_DECLARE(mouse_cursor_icon); /*Declare the image file.*/
    lv_obj_t * cursor_obj;
    cursor_obj = lv_image_create(lv_screen_active()); /*Create an image object for the cursor */
    lv_image_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
    lv_indev_set_cursor(mouse, cursor_obj);             /*Connect the image  object to the driver*/

    lv_indev_t * mousewheel = lv_sdl_mousewheel_create();
    lv_indev_set_display(mousewheel, disp);
    lv_indev_set_group(mousewheel, lv_group_get_default());

#elif (LV_USE_WINDOWS)
  wchar_t *title = L"Windwos LVGL";
  lv_display_t *disp = lv_windows_create_display(title, w, h, 100, true, true);

  HWND hWnd = lv_windows_get_indev_window_handle(NULL); 
  lv_indev_t * pointer_indev = lv_windows_acquire_pointer_indev(disp);
  if (pointer_indev == NULL)
  {
    MessageBox(hWnd, TEXT("Failed to acquire pointer input device!"), TEXT("Error"), MB_OK | MB_ICONERROR);
  }
  LV_IMAGE_DECLARE(mouse_cursor_icon); /*Declare the image file.*/
  lv_obj_t * cursor_obj;
  cursor_obj = lv_image_create(lv_screen_active()); /*Create an image object for the cursor */
  lv_image_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
  lv_indev_set_cursor(pointer_indev, cursor_obj); 
  lv_indev_enable(pointer_indev, true);

//   lv_indev_t * kb = lv_windows_acquire_keypad_indev(disp);
#elif (LV_USE_WAYLAND)
    char *title = "wayland lvgl";
    
    reemoon_info ("window_width: %d, window_height: %d\n", SCREEN_INFO.DISPLAY_W, SCREEN_INFO.DISPLAY_H);
    disp = lv_wayland_window_create(SCREEN_INFO.DISPLAY_W, SCREEN_INFO.DISPLAY_H, title, lv_wayland_display_close_cb);
    lv_wayland_window_set_fullscreen(disp, true);

#endif

    rm_kb = lv_keyboard_create(lv_layer_sys());
    lv_obj_add_flag(rm_kb, LV_OBJ_FLAG_HIDDEN);
    lv_obj_set_style_text_font(rm_kb, LV_FONT_DEFAULT, 0);
    rm_kb_num = lv_keyboard_create(lv_layer_sys());
    lv_keyboard_set_mode(rm_kb_num, LV_KEYBOARD_MODE_NUMBER);
    lv_obj_add_flag(rm_kb_num, LV_OBJ_FLAG_HIDDEN);
    lv_obj_set_style_text_font(rm_kb_num, LV_FONT_DEFAULT, 0);
	return disp;
}

static void get_system_conf (void)
{
    // system_info = (SYSTEM_INFO *)malloc(sizeof(system_info));
    system_info = &system_info_;
    if (system_info == NULL)
    {
        reemoon_error("Memory allocation failed for system_info\n");
        exit(0);
    }

    int data_size = 1024 * sizeof(char);
    char *data_buff = malloc(data_size);

    // 读取默认的配置信息
    int size = reemoon_read_file(SYSTEM_CONFIG_FILE, data_buff, data_size) ;
    if (size>0)
        parse_sysconf_JSON(data_buff);
    memset (data_buff, 0, data_size);
    // 读取页面配置u信息
    if (system_info->language == LANGUAGE_CN)
    {
        reemoon_info ("get page config <%s>\n", SYSTEM_CONFIG_CN_FILE);
        size = reemoon_read_file(SYSTEM_CONFIG_CN_FILE, data_buff, data_size) ;
    }
    else  if (system_info->language == LANGUAGE_EN)
    {
        reemoon_info ("get page config <%s>\n", SYSTEM_CONFIG_EN_FILE);
        size = reemoon_read_file(SYSTEM_CONFIG_EN_FILE, data_buff, data_size) ;
    }
    if (size>0)
        parse_ui_JSON(data_buff);
    memset (data_buff, 0, data_size);
    free (data_buff);
}

int main(int argc, char **argv)
{
    // 监听系统退出
	signal(SIGINT , sighandler_handler); 

	// log日志库初始化
	mzk_log_init();
	// 示例1：使用api进行配置；只启用INFO和ERROR日志
	mzk_log_clean_all_config();
	// 配置日志库
	mzk_log_set_types (MZK_LOG_TYPE_ALL, true);
	mzk_log_set_min_level(MZK_LOG_LEVEL_ALL);
	mzk_log_set_autonewline(true);
	mzk_log_set_modules(MZK_LOG_MODULE_TERMINAL | MZK_LOG_MODULE_FILE | MZK_LOG_MODULE_NETWORK, true);
	mzk_log_set_format("[L]-[T]-[F:N]: ");
	// 文件存储
	mzk_log_filemodule_init("./log", "lht", LOG_STORE_SINGLE, 2*1024*1024, 10);
	char buff[20];
	mzk_log_get_types_name(mzk_log_get_types(), buff, sizeof(buff));
	MZK_LOG_INFO("%s",buff);

    // 获取系统配置
	reemoon_info ("get system config <%s>\n", SYSTEM_CONFIG_FILE);
	get_system_conf();
	// lvgl 初始化
	lv_init();
    reemoon_info("lvgl init\n");
    // 显示器 鼠标 键盘初始化
    if (hal_init() == NULL) {
        reemoon_warn ("Please select a development platform: WINDOWS/LINUX/ARM\n");
	    lv_deinit();
        return -1;
    }

    // UI 界面
    lv_obj_t *parent = NULL;
    parent = UI_Init();
    reemoon_info("UI_Init init\n");

	pthread_t lv_pth_id;
	pthread_create (&lv_pth_id, NULL, lv_pth_tick, NULL);

    // RabbitMQ 初始化
#if RABBITMQ_ENABLE
    if (parent != NULL)
        rabbitmq_init(); 
    reemoon_info("rabbitmq init\n");
#endif

#if ARM_ENABLE
    print_io_ctrl_init();
    reemoon_info("io init\n");
    wifi_app_init();
    reemoon_info("wifi init\n");
#endif

    reemoon_info ("system running ...\n");
	while (SYSTEM_RUNNING_FALG) {
        usleep(100*1000);
        if (RABBIT_STAUTS == false)
        {
            #if RABBITMQ_ENABLE
                if (parent != NULL)
                rabbitmq_init(); 
            #endif
        }
  	}
	pthread_join(lv_pth_id, NULL);
	lv_deinit();
    reemoon_info("ui deinit\n");
    print_io_ctrl_deinit();

	// 日志库
	mzk_log_deinit();

    wifi_fifo_deinit();
#if RABBITMQ_ENABLE
    rabbitmq_deinit();
#endif
#if LV_USE_LINUX_FBDEV
    rm_fbdev_deinit();
    mouse_evdev_deinit();
#endif
    reemoon_info("system exit ...\n");
    SYSTEM_END_FALG = true;
    while(1) {
        if (SYSTEM_SIG_EIXT_FALG)
            break;
    }
  	return 0;
}
